package com.yinshu.teleshield.tkbase.controller;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.yinshu.teleshield.tkbase.base.PageResult;
import com.yinshu.teleshield.tkbase.base.R;
import com.yinshu.teleshield.tkbase.base.entity.TKSuperEntity;
import com.yinshu.teleshield.tkbase.mybatis.conditions.Wraps;
import com.yinshu.teleshield.tkbase.request.PageParams;
import com.yinshu.teleshield.utils.CommonBeanUtils;
import com.yinshu.teleshield.utils.PageResultUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.media.Schema;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.io.Serializable;
import java.util.Collections;
import java.util.List;
import java.util.Objects;

/**
 * 查询Controller
 *
 * @param <Entity>    实体
 * @param <Id>        主键
 * @param <PageQuery> 分页参数
 * @param <ResultVO>  实体返回VO
 * @author yangjunxiong
 */
public interface QueryController<Id extends Serializable, Entity extends TKSuperEntity<Id>, PageQuery, ResultVO>
        extends PageController<Id, Entity, PageQuery, ResultVO> {
    Logger log = LoggerFactory.getLogger(QueryController.class);
    /**
     * 单体查询
     *
     * @param id 主键id
     * @return 查询结果
     */
    @Parameters({
            @Parameter(name = "id", description = "主键", schema = @Schema(type = "long"), in = ParameterIn.PATH),
    })
    @Operation(summary = "单体查询", description = "单体查询")
    @GetMapping("/{id}")
//    @WebLog("'查询:' + #id")
    default R<ResultVO> get(@PathVariable Id id) {
        log.info("{}.get.单体查询,请求参数:id:{}", this.getClass().getName(), id);
        Entity entity = getSuperService().getById(id);
        ResultVO resultVO = CommonBeanUtils.toBean(entity, getResultVOClass());
        if (Objects.isNull(resultVO)) {
            return success(resultVO);
        }
        return success(resultVO);
    }

    /**
     * 查询详情
     *
     * @param id 主键id
     * @return 查询结果
     */
    @Operation(summary = "查询单体详情", description = "查询单体详情")
    @GetMapping("/detail")
//    @WebLog("'查询:' + #id")
    default R<ResultVO> getDetail(@RequestParam("id") Id id) {
        log.info("{}.getDetail.查询单体详情,请求参数:id:{}", this.getClass().getName(), id);
        Entity entity = getSuperService().getById(id);
        ResultVO resultVO = CommonBeanUtils.toBean(entity, getResultVOClass());
        if (Objects.isNull(resultVO)) {
            return success(resultVO);
        }
        this.getSuperService().handlerListResult(Collections.singletonList(resultVO));
        return success(resultVO);
    }

    /**
     * 批量查询
     *
     * @param params 批量查询
     * @return 查询结果
     */
//    @OperateLog(name = "批量查询", type = {OperateTypeEnum.GET}, logResultData = false)
    @Operation(summary = "批量查询", description = "批量查询")
    @PostMapping("/queryList")
//    @WebLog("批量查询")
    default R<PageResult<ResultVO>> queryList(@RequestBody @Validated PageParams<PageQuery> params) {
        log.info("{}.queryList.批量查询,请求参数:params:{}", this.getClass().getName(), CommonBeanUtils.toJSONString(params));
        MPJLambdaWrapper<Entity> wrapper = Wraps.q(params.getExtra(), getEntityClass());
        getSuperService().handlerWrapper(wrapper, params);
        IPage<Entity> pageEntity = getSuperService().getSuperManager()
                .getSuperMapper().selectPage(params.buildPage(), wrapper);
        IPage<ResultVO> pageResultVO = CommonBeanUtils.toBeanPage(pageEntity, getResultVOClass());
        getSuperService().handlerListResult(pageResultVO.getRecords());
        return success(PageResultUtils.toPageResult(pageResultVO));
    }

    /**
     * 批量查询
     *
     * @param ids 批量查询
     * @return 查询结果
     */
//    @OperateLog(name = "根据Id批量查询", type = {OperateTypeEnum.GET}, logResultData = false)
    @Operation(summary = "根据Id批量查询", description = "根据Id批量查询")
    @PostMapping("/findByIds")
//    @WebLog("根据Id批量查询")
    default R<List<ResultVO>> findByIds(@RequestBody List<Id> ids) {
        log.info("{}.findByIds.根据Id批量查询,请求参数:ids:{}", this.getClass().getName(), CommonBeanUtils.toJSONString(ids));
        if (CollUtil.isEmpty(ids)) {
            return R.success(Collections.emptyList());
        }
        List<Entity> list = getSuperService().listByIds(ids);
        if (CollUtil.isEmpty(list)) {
            return R.success(Collections.emptyList());
        }
        List<ResultVO> resultVOs = CommonBeanUtils.toList(getResultVOClass(), list);
        this.getSuperService().handlerListResult(resultVOs);
        return success(resultVOs);
    }
}
